package com.example.server.controller.admin;

import com.example.server.common.config.annotation.MustLogin;
import com.example.server.common.config.auth.TokenUser;
import com.example.server.common.constant.Result;
import com.example.server.service.admin.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-20 10:12
 */
@Api(tags = "Admin角色相关接口")
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {
    @Resource
    RoleService roleService;

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "查询角色", notes = "查询角色")
    @GetMapping("/query")
    public Result queryRole(@ApiIgnore TokenUser tokenUser) {
        return roleService.queryRole(tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "更新角色菜单按钮", notes = "更新角色菜单按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
            @ApiImplicitParam(value = "菜单按钮 json格式", name = "jsonData", required = true)
    })
    @PostMapping("/updateMenu")
    public Result updateMenu(Integer roleId, String jsonData, @ApiIgnore TokenUser tokenUser) {
        return roleService.updateMenu(roleId, jsonData, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色名称", name = "name", required = true)
    })
    @PostMapping("/add")
    public Result addRole(String name, @ApiIgnore TokenUser tokenUser) {
        return roleService.addRole(name, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "更新角色", notes = "更新角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
            @ApiImplicitParam(value = "角色名称", name = "name", required = true)
    })
    @PostMapping("/update")
    public Result updateRole(Integer roleId, String name, @ApiIgnore TokenUser tokenUser) {
        return roleService.updateRole(roleId, name, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true)
    })
    @PostMapping("/delete")
    public Result deleteRole(Integer roleId, @ApiIgnore TokenUser tokenUser) {
        return roleService.deleteRole(roleId, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "启/禁用角色", notes = "启/禁用角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
            @ApiImplicitParam(value = "启用/禁用状态", name = "enable", required = true)
    })
    @PostMapping("/setEnable")
    public Result setEnable(Integer roleId, Integer enable, @ApiIgnore TokenUser tokenUser) {
        return roleService.setEnable(roleId, enable, tokenUser.getCustomerId());
    }
}